home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Mark Pilgrim / Jotto ][ 1.2 / source / Shell ƒ / styled text.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  6.9 KB  |  286 lines  |  [TEXT/MMCC]

  1. #include "styled text.h"
  2.  
  3. void DrawTheText(CharHandle theText, StylHandle theStyleHandle, short theJust,
  4.     short theMode, Rect theRect)
  5. {
  6.     short            i, numStyles;
  7.     long            textPos;
  8.     long            maxOffset;
  9.     Str255            thisLine;
  10.     Boolean            notDoneYet;
  11.     unsigned char    thisChar;
  12.     short            theRow, theCol;
  13.     unsigned char    lastEnd, thisEnd;
  14.     Boolean            overRun;
  15.     
  16.     numStyles=(**theStyleHandle).numStyles;
  17.     textPos=0L;
  18.     theRow=theRect.top+(**theStyleHandle).theStyle[0].fontDescent+1;
  19.     theCol=theRect.left;
  20.     thisLine[0]=0x00;
  21.     lastEnd=0;
  22.     for (i=0; i<numStyles; i++)
  23.     {
  24.         if (i==numStyles-1)
  25.             maxOffset=GetHandleSize((Handle)theText);
  26.         else
  27.             maxOffset=(**theStyleHandle).theStyle[i+1].offset;
  28.         
  29.         TextFont((**theStyleHandle).theStyle[i].fontNum);
  30.         TextFace((**theStyleHandle).theStyle[i].fontStyle);
  31.         TextSize((**theStyleHandle).theStyle[i].fontSize);
  32.         TextMode(theMode);
  33.         
  34.         while (textPos<maxOffset)
  35.         {
  36.             notDoneYet=TRUE;
  37.             while ((textPos<maxOffset) && (notDoneYet))
  38.             {
  39.                 thisChar=thisLine[++thisLine[0]]=(*theText)[textPos++];
  40.                 notDoneYet=((thisChar!=' ') && (thisChar!=0x0d));
  41.             }
  42.             
  43.             thisEnd=thisLine[0];
  44.             overRun=(theRect.right-theCol<=StringWidth(thisLine));
  45.             
  46.             if ((overRun) || (thisChar==0x0d) || (textPos==maxOffset))
  47.             {
  48.                 if (overRun)
  49.                     thisLine[0]=lastEnd;
  50.                 if (theJust==kCenter)
  51.                     MoveTo((theRect.right-theRect.left-StringWidth(thisLine))/2+
  52.                             theCol, theRow);
  53.                 else
  54.                     MoveTo(theCol, theRow);
  55.                 theCol+=StringWidth(thisLine);
  56.                 DrawString(thisLine);
  57.                 if (overRun)
  58.                 {
  59.                     BlockMove(&thisLine[lastEnd+1], &thisLine[1], thisEnd-lastEnd+1);
  60.                     if (thisEnd>=lastEnd)
  61.                     {
  62.                         thisLine[0]=thisEnd-lastEnd-1;
  63.                         textPos--;
  64.                     }
  65.                     else
  66.                         thisEnd=thisLine[0]=0x00;
  67.                 }
  68.                 else thisLine[0]=0x00;
  69.                 if ((overRun) || (thisChar==0x0d))
  70.                 {
  71.                     theRow+=(**theStyleHandle).theStyle[i].lineHeight;
  72.                     theCol=theRect.left;
  73.                 }
  74.             }
  75.             
  76.             lastEnd=thisEnd;
  77.         }
  78.         
  79.         if (thisLine[0]!=0x00)
  80.         {
  81.             if (theJust==kCenter)
  82.                 MoveTo((theRect.right-theRect.left-StringWidth(thisLine))/2+
  83.                         theCol, theRow);
  84.             else
  85.                 MoveTo(theCol, theRow);
  86.             theCol+=StringWidth(thisLine);
  87.             DrawString(thisLine);
  88.             thisLine[0]=0x00;
  89.         }
  90.     }
  91.     TextMode(srcOr);
  92. }
  93.  
  94. void DrawThePartialText(CharHandle theText, StylHandle theStyleHandle, short theJust,
  95.     short theMode, Rect theRect, short startLine, short endLinePlusOne)
  96. {
  97.     short            i, numStyles;
  98.     long            textPos;
  99.     long            maxOffset;
  100.     Str255            thisLine;
  101.     Boolean            notDoneYet;
  102.     unsigned char    thisChar;
  103.     short            theRow, theCol;
  104.     unsigned char    lastEnd, thisEnd;
  105.     Boolean            overRun;
  106.     short            thisLineIndex;
  107.     
  108.     thisLineIndex=0;
  109.     numStyles=(**theStyleHandle).numStyles;
  110.     textPos=0L;
  111.     theRow=theRect.top+(**theStyleHandle).theStyle[0].fontDescent+1;
  112.     theCol=theRect.left;
  113.     thisLine[0]=0x00;
  114.     lastEnd=0;
  115.     for (i=0; i<numStyles; i++)
  116.     {
  117.         if (i==numStyles-1)
  118.             maxOffset=GetHandleSize((Handle)theText);
  119.         else
  120.             maxOffset=(**theStyleHandle).theStyle[i+1].offset;
  121.         
  122.         TextFont((**theStyleHandle).theStyle[i].fontNum);
  123.         TextFace((**theStyleHandle).theStyle[i].fontStyle);
  124.         TextSize((**theStyleHandle).theStyle[i].fontSize);
  125.         TextMode(theMode);
  126.         
  127.         while (textPos<maxOffset)
  128.         {
  129.             notDoneYet=TRUE;
  130.             while ((textPos<maxOffset) && (notDoneYet))
  131.             {
  132.                 thisChar=thisLine[++thisLine[0]]=(*theText)[textPos++];
  133.                 notDoneYet=((thisChar!=' ') && (thisChar!=0x0d));
  134.             }
  135.             
  136.             thisEnd=thisLine[0];
  137.             overRun=(theRect.right-theCol<=StringWidth(thisLine));
  138.             
  139.             if ((overRun) || (thisChar==0x0d) || (textPos==maxOffset))
  140.             {
  141.                 if (overRun)
  142.                     thisLine[0]=lastEnd;
  143.                 if (theJust==kCenter)
  144.                     MoveTo((theRect.right-theRect.left-StringWidth(thisLine))/2+
  145.                             theCol, theRow);
  146.                 else
  147.                     MoveTo(theCol, theRow);
  148.                 theCol+=StringWidth(thisLine);
  149.                 if ((thisLineIndex>=startLine) && (thisLineIndex<endLinePlusOne))
  150.                     DrawString(thisLine);
  151.                 if (overRun)
  152.                 {
  153.                     BlockMove(&thisLine[lastEnd+1], &thisLine[1], thisEnd-lastEnd+1);
  154.                     if (thisEnd>=lastEnd)
  155.                     {
  156.                         thisLine[0]=thisEnd-lastEnd-1;
  157.                         textPos--;
  158.                     }
  159.                     else
  160.                         thisEnd=thisLine[0]=0x00;
  161.                 }
  162.                 else thisLine[0]=0x00;
  163.                 if ((overRun) || (thisChar==0x0d))
  164.                 {
  165.                     if ((thisLineIndex>=startLine) && (thisLineIndex<endLinePlusOne))
  166.                         theRow+=(**theStyleHandle).theStyle[i].lineHeight;
  167.                     theCol=theRect.left;
  168.                     thisLineIndex++;
  169.                 }
  170.             }
  171.             
  172.             lastEnd=thisEnd;
  173.         }
  174.         
  175.         if (thisLine[0]!=0x00)
  176.         {
  177.             if (theJust==kCenter)
  178.                 MoveTo((theRect.right-theRect.left-StringWidth(thisLine))/2+
  179.                         theCol, theRow);
  180.             else
  181.                 MoveTo(theCol, theRow);
  182.             theCol+=StringWidth(thisLine);
  183.             if ((thisLineIndex>=startLine) && (thisLineIndex<endLinePlusOne))
  184.                 DrawString(thisLine);
  185.             thisLine[0]=0x00;
  186.         }
  187.     }
  188.     TextMode(srcOr);
  189. }
  190.  
  191. short CountLines(CharHandle theText, StylHandle theStyleHandle, short theJust,
  192.     short theMode, Rect theRect)
  193. {
  194.     short            i, numStyles;
  195.     long            textPos;
  196.     long            maxOffset;
  197.     Str255            thisLine;
  198.     Boolean            notDoneYet;
  199.     unsigned char    thisChar;
  200.     short            theRow, theCol;
  201.     unsigned char    lastEnd, thisEnd;
  202.     Boolean            overRun;
  203.     short            numLines;
  204.     
  205.     numLines=0;
  206.     numStyles=(**theStyleHandle).numStyles;
  207.     textPos=0L;
  208.     theRow=theRect.top+(**theStyleHandle).theStyle[0].fontDescent+1;
  209.     theCol=theRect.left;
  210.     thisLine[0]=0x00;
  211.     lastEnd=0;
  212.     for (i=0; i<numStyles; i++)
  213.     {
  214.         if (i==numStyles-1)
  215.             maxOffset=GetHandleSize((Handle)theText);
  216.         else
  217.             maxOffset=(**theStyleHandle).theStyle[i+1].offset;
  218.         
  219.         TextFont((**theStyleHandle).theStyle[i].fontNum);
  220.         TextFace((**theStyleHandle).theStyle[i].fontStyle);
  221.         TextSize((**theStyleHandle).theStyle[i].fontSize);
  222.         TextMode(theMode);
  223.         
  224.         while (textPos<maxOffset)
  225.         {
  226.             notDoneYet=TRUE;
  227.             while ((textPos<maxOffset) && (notDoneYet))
  228.             {
  229.                 thisChar=thisLine[++thisLine[0]]=(*theText)[textPos++];
  230.                 notDoneYet=((thisChar!=' ') && (thisChar!=0x0d));
  231.             }
  232.             
  233.             thisEnd=thisLine[0];
  234.             overRun=(theRect.right-theCol<=StringWidth(thisLine));
  235.             
  236.             if ((overRun) || (thisChar==0x0d) || (textPos==maxOffset))
  237.             {
  238.                 if (overRun)
  239.                     thisLine[0]=lastEnd;
  240. //                if (theJust==kCenter)
  241. //                    MoveTo((theRect.right-theRect.left-StringWidth(thisLine))/2+
  242. //                            theCol, theRow);
  243. //                else
  244. //                    MoveTo(theCol, theRow);
  245.                 theCol+=StringWidth(thisLine);
  246. //                DrawString(thisLine);
  247.                 if (overRun)
  248.                 {
  249.                     BlockMove(&thisLine[lastEnd+1], &thisLine[1], thisEnd-lastEnd+1);
  250.                     if (thisEnd>=lastEnd)
  251.                     {
  252.                         thisLine[0]=thisEnd-lastEnd-1;
  253.                         textPos--;
  254.                     }
  255.                     else
  256.                         thisEnd=thisLine[0]=0x00;
  257.                 }
  258.                 else thisLine[0]=0x00;
  259.                 if ((overRun) || (thisChar==0x0d))
  260.                 {
  261.                     theRow+=(**theStyleHandle).theStyle[i].lineHeight;
  262.                     theCol=theRect.left;
  263.                     numLines++;
  264.                 }
  265.             }
  266.             
  267.             lastEnd=thisEnd;
  268.         }
  269.         
  270.         if (thisLine[0]!=0x00)
  271.         {
  272. //            if (theJust==kCenter)
  273. //                MoveTo((theRect.right-theRect.left-StringWidth(thisLine))/2+
  274. //                        theCol, theRow);
  275. //            else
  276. //                MoveTo(theCol, theRow);
  277.             theCol+=StringWidth(thisLine);
  278. //            DrawString(thisLine);
  279.             thisLine[0]=0x00;
  280.         }
  281.     }
  282.     TextMode(srcOr);
  283.     
  284.     return numLines;
  285. }
  286.